<?php
/**
 * 用户接口
 * @copyright Copyright &copy; 2011, MAOJIAN
 * @since 1.0 - 2011-7-16
 * @author maojianlw@139.com
 */

namespace Admin\Controller;
class CheckController extends BaseController{
	
	private $user_model;
	
	public function _initialize(){
		$this->user_model = getInstance('Admin\Model\ManagerModel');;
	}
	
	/**
	 * 登录接口
	 */
    public function login(){
    	$username = $_POST['username'];
    	$password = $_POST['password'];
    	$verify = $_POST['verify'];
    	if(empty($username)){
    		$this->returnJson(201);
    	}else if(empty($password)){
    		$this->returnJson(202);
     	}else if(empty($verify)){
    		$this->returnJson(206);
		}else if($_SESSION['VerifyCode'] != $verify){
    		$this->returnJson(207);
    	}else{
    		$password = md5($password);
    		if($manager_info = $this->user_model->where("username='$username' AND password='$password'")->select()){
    			if($manager_info[0]['state'] == 'D'){
    				$this->returnJson(203);
    			}else{
					unset($_SESSION['VerifyCode']);
    				//$role_info = M('role')->field('name')->where("id={$manager_info['role_id']}")->find();
    				//$manager_info['role_name'] = $role_info['name'];
    				$_SESSION['SESSION_USER'] = (array)$manager_info;
    				$this->user_model->where("uid={$manager_info['uid']}")->update(array('login_ip'=>get_client_ip(), 'login_time'=>date('Y-m-d H:i:s')));
    				$this->returnJson(200, $manager_info);
    			}
    		}else{
    			$this->returnJson(204);
    		}
    	}
    }
    
    
    /**
     * 返回角色配置XML文档
     */
    public function roleConfigAction(){
    	$role_id = (int)$_REQUEST['role_id'];
    	if(empty($role_id)){
    		$role_id = (int)$_SESSION[SESSION_USER_NAME]['role_id'];
    	}
    	
    	if($role_id){
    		// 读取角色配置文件
	    	$role_info = M('role')->field('config')->where("id=$role_id")->find();
	    	$config = trim($role_info['config']);
	    	if($config){
	    		header('Content-Type:text/xml;charset=utf-8');
	    		exit($config);
	    	}
    	}
    }
  
    
    /**
     * 根据用户会话key获取值
     */
    public function getSessionAction(){
    	$key = $_REQUEST['key'];
    	$manager_info = $_SESSION[SESSION_USER_NAME];
    	if(!empty($key) && $manager_info){
    		$keys = explode('|', $key);
    		foreach($keys as $key){
    			if(array_key_exists($key, $manager_info)){
    				$data[$key] = $manager_info[$key];
    			}
    		}
    		if($data)
    			$this->formatReturn(200, $data);
    	}
    	$this->formatReturn(205);
    }
    
    
    
    /**
	 * 用户退出
	 */
	public function logoutAction() {
		unset ($_SESSION[SESSION_USER_NAME]);
		session_destroy();
		$this->formatReturn(200);
	}
    
    /**
     * 按照某种格式返回数据
     */
    public function returnJson($code, $list=''){
		$message = array(
			200 => '操作成功',
			201 => '用户名为空',
			202 => '用户密码为空',
			203 => '用户已被锁定',
			204 => '用户名或密码错误',
			205 => '用户会话key不存在',
			206 => '验证码为空',
			207 => '验证码错误',
			208 => '参数有误'
		);
    	$data = array('statusCode'=>$code, 'message'=>$message[$code], 'sessionId'=>session_id());
    	if(!empty($list)){
    		$data['data'] = $list;
    	}
    	exit(json_encode($data));
    }
    
}
?>